package ro.upt.dcda.web.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jms.StreamMessage;

import ro.upt.dcda.pricezone.common.Config;
import ro.upt.dcda.pricezone.common.JMSMediator;
import ro.upt.dcda.pricezone.common.Server;
import ro.upt.dcda.pricezone.common.StoreData;
import ro.upt.dcda.pricezone.common.Config.ServerInfo;
import ro.upt.dcda.pricezone.s1.pojo.Admin;
import ro.upt.dcda.pricezone.s1.pojo.Product;
import ro.upt.dcda.pricezone.s1.requests.GetProductsRequest;

import com.opensymphony.xwork2.ActionContext;

public class AdminNewProductInfo {

	private String id;
	private String server;
	private String productName;
	private String price;
	private String currency;
	private List<String> servers;

	public String execute() throws Exception {
		System.out.println("Creating new price information.");
		Map<String, Object> session = ActionContext.getContext().getSession();
		Admin admin = (Admin) session.get("admin");
		String adminSid = (String) session.get("adminSid");
		if (admin == null || adminSid == null) {
			return "noAuth";
		}
		servers = new ArrayList<String>();
		for (ServerInfo serverInfo : Config.getServerList()) {
			servers.add(serverInfo.getName());
		}
		int storeId;
		try {
			storeId = Integer.valueOf(id);
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			return "def";
		}
		try {
			GetProductsRequest request = new GetProductsRequest();
			List<Product> allProductsData = request.work();
			int productId = -1;
			for (Product temp : allProductsData) {
				if (temp.getName().equals(productName)) {
					productId = temp.getId();
				}
			}
			if (productId == -1) {
				return "def";
			}
			Server s = JMSMediator.getMediator().getServerByName(this.server);
			StreamMessage msg = s.createStreamMessage();
			msg.writeString("newPrice");
			msg.writeInt(storeId);
			msg.writeInt(productId);
			msg.writeString(price);
			msg.writeString(currency);
			StreamMessage response = null;
			response = (StreamMessage) s.getResponseFor(msg, 30000);
			if (response == null) {
				return "def";
			}
			if (response.readInt() == 1) {
				System.out.println("New price saved");
			} else {
				System.out.println("New price not saved");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "def";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public List<StoreData> getStores() {
		return new ArrayList<StoreData>();
	}

	public void setStores(List<StoreData> stores) {
	}

	public String getDefaultServers() {
		return servers.size() > 0 ? servers.get(0) : "";
	}

	public List<String> getServers() {
		return servers;
	}

	public void setServers(List<String> servers) {
		this.servers = servers;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}
}
